package com.banciyuan.bcywebview.biz.circles.adapter;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.circles.CircleMoreUserActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.fragment.CircleSmoothUserTopFragment;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.ActiveUser;

/**
 * Created by squall on 2016/6/13.
 */
public class CircleUserAdapter extends BaseAdapter {

    private static final int TITLE = 0;
    private static final int USER = 1;
    private static final int USER_TOP = 3;
    private static final int MORE = 2;

    private List<ActiveUser> alist;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private Map<String, Integer> map = new HashMap<>();
    private String id;

    public CircleUserAdapter(Context mContext, List<ActiveUser> alist, Map<String, Integer> map, String id) {
        this.alist = alist;
        this.map = map;
        this.id = id;
        this.mContext = mContext;
        layoutInflater = LayoutInflater.from(mContext);
    }

    public CircleUserAdapter(Context mContext, List<ActiveUser> alist) {
        this.alist = alist;
        this.mContext = mContext;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (alist.get(position).getType().contains("title")) {
            return TITLE;
        } else if (alist.get(position).getType().contains("more")) {
            return MORE;
        } else {
            if (alist.get(position).getIndex() == 1) {
                return USER_TOP;
            } else {
                return USER;
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getCount() {
        return alist.size();
    }

    private class ViewHolder {
        private ImageView mark;
        private RoundedImageView head_iv;
        private TextView uname_tv;
        private TextView production_tv;
        private TextView fansCount_tv;
        private TextView foucs_tv;
        private ImageView[] pics;
        private TextView[] plains;
        private TextView[] titles;
        private LinearLayout[] containers;

        public ViewHolder(View convertView) {
            mark = (ImageView) convertView.findViewById(R.id.rank_mark);
            head_iv = (RoundedImageView) convertView.findViewById(R.id.user_head);
            uname_tv = (TextView) convertView.findViewById(R.id.user_name);
            production_tv = (TextView) convertView.findViewById(R.id.production_tv);
            fansCount_tv = (TextView) convertView.findViewById(R.id.fanscount_tv);
            foucs_tv = (TextView) convertView.findViewById(R.id.user_focus);
            pics = new ImageView[]{(ImageView) convertView.findViewById(R.id.pic_one),
                    (ImageView) convertView.findViewById(R.id.pic_two),
                    (ImageView) convertView.findViewById(R.id.pic_three)};
            plains = new TextView[]{(TextView) convertView.findViewById(R.id.plain_one),
                    (TextView) convertView.findViewById(R.id.plain_two),
                    (TextView) convertView.findViewById(R.id.plain_three)};

            titles = new TextView[]{(TextView) convertView.findViewById(R.id.title_one),
                    (TextView) convertView.findViewById(R.id.title_two),
                    (TextView) convertView.findViewById(R.id.title_three)};

            containers = new LinearLayout[]{(LinearLayout) convertView.findViewById(R.id.tv_container_one),
                    (LinearLayout) convertView.findViewById(R.id.tv_container_two),
                    (LinearLayout) convertView.findViewById(R.id.tv_container_three)};
        }
    }

    private class ViewTitle {
        private TextView title;
        private LinearLayout empty;

        public ViewTitle(android.view.View convertView) {
            title = (TextView) convertView.findViewById(R.id.label_title);
            empty = (LinearLayout) convertView.findViewById(R.id.empty_view);
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewTitle viewTitle = null;
        if (convertView == null) {
            if (getItemViewType(position) == USER) {
                convertView = layoutInflater.inflate(R.layout.circle_top_user_card, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else if (getItemViewType(position) == TITLE) {
                convertView = layoutInflater.inflate(R.layout.circle_user_top_header, null);
                viewTitle = new ViewTitle(convertView);
                convertView.setTag(viewTitle);
            } else if (getItemViewType(position) == USER_TOP) {
                convertView = layoutInflater.inflate(R.layout.circle_top_user_card_champion, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                convertView = layoutInflater.inflate(R.layout.circle_user_more, null);
            }
        } else {
            if (getItemViewType(position) == USER) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else if (getItemViewType(position) == TITLE) {
                viewTitle = (ViewTitle) convertView.getTag();
            } else if (getItemViewType(position) == USER_TOP) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = layoutInflater.inflate(R.layout.circle_user_more, null);
            }
        }
        final ActiveUser activeUser = alist.get(position);
        if ((getItemViewType(position) == USER_TOP || getItemViewType(position) == USER) && viewHolder != null) {
            if (!TextUtils.isEmpty(activeUser.getAvatar())) {
                ImageLoader.getInstance().displayImage(activeUser.getAvatar(), viewHolder.head_iv, ImageOptions.optionHead);
            }
            if (activeUser.getIndex() == 2) {
                viewHolder.head_iv.setBorderWidth((float) UIUtils.dip2px(2, mContext));
                viewHolder.mark.setVisibility(View.VISIBLE);
                viewHolder.head_iv.setBorderColor(mContext.getResources().getColorStateList(R.color.top_two_red));
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back, mContext.getTheme()));
                } else {
                    viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back));
                }
            } else if (activeUser.getIndex() == 3) {
                viewHolder.head_iv.setBorderWidth((float) UIUtils.dip2px(2, mContext));
                viewHolder.mark.setVisibility(View.VISIBLE);
                viewHolder.head_iv.setBorderColor(mContext.getResources().getColorStateList(R.color.top_three_yellow));
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back, mContext.getTheme()));
                } else {
                    viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back));
                }
            } else if (activeUser.getIndex() == 0) {
                viewHolder.mark.setVisibility(View.GONE);
                viewHolder.head_iv.setBorderWidth(0f);
            }
            if (!TextUtils.isEmpty(activeUser.getUname())) {
                viewHolder.uname_tv.setText(activeUser.getUname());
            }
            if (!TextUtils.isEmpty(activeUser.getFollower())) {
                viewHolder.fansCount_tv.setText(activeUser.getFollower());
            }
            if (!TextUtils.isEmpty(activeUser.getPost_count())) {
                viewHolder.production_tv.setText(activeUser.getPost_count());
            }
            List<ActiveUser.UserPost> ulist = activeUser.getPosts();
            if (ulist != null && !ulist.isEmpty()) {
                for (int i = 0; i < ulist.size(); i++) {
                    ActiveUser.UserPost u = ulist.get(i);
                    if (i < viewHolder.pics.length) {
                        if (!TextUtils.isEmpty(u.getCover())) {
                            ImageLoader.getInstance().displayImage(u.getCover(), viewHolder.pics[i], ImageOptions.options);
                        }
                        if (!TextUtils.isEmpty(u.getTitle())) {
                            viewHolder.containers[i].setVisibility(View.VISIBLE);
                            viewHolder.titles[i].setText(Html.fromHtml(u.getTitle()));
                        } else {
                            viewHolder.containers[i].setVisibility(View.GONE);
                        }
                        if (!TextUtils.isEmpty(u.getPlain())) {
                            viewHolder.plains[i].setText(Html.fromHtml(u.getPlain()));
                        }
                    }
                    final int finalI = i;
                    viewHolder.pics[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ActiveUser.UserPost userPost = activeUser.getPosts().get(finalI);
                            if (activeUser.getType().equals(CircleSmoothUserTopFragment.COSER)) {
                                DetailDataHelper.gotoCosDetail(mContext, userPost.getRp_id(), userPost.getCp_id(), false);
                            } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.DRAWER)) {
                                DetailDataHelper.gotoDrawerDetail(mContext, userPost.getRp_id(), userPost.getDp_id(), false);
                            } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.WRITER)) {
                                DetailDataHelper.gotoWriterDetail(mContext, userPost.getRp_id(), userPost.getWp_id(), false);
                            }
                        }
                    });
                }
            }
            if (activeUser.getFollowstate().equals("unfollow") && !StringUtil.notNullEqual(activeUser.getUid(), UserDataHelper.getInstance(mContext).getUid())) {
                viewHolder.foucs_tv.setSelected(false);
                viewHolder.foucs_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        if (!v.isSelected()) {
                            TimelineLikeHelper.doFollow(mContext, "dofollow", activeUser.getUid(), new TimelineLikeHelper.FollowSuccess() {
                                @Override
                                public void onFollowSuccess(String response) {
                                    MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                                    v.setSelected(true);
                                    activeUser.setFollowstate("havafollow");
                                }
                            });
                        }
                    }
                });
            } else {
                viewHolder.foucs_tv.setVisibility(View.GONE);
            }
            View.OnClickListener goPc = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, activeUser.getUid());
                }
            };
            viewHolder.head_iv.setOnClickListener(goPc);
            viewHolder.uname_tv.setOnClickListener(goPc);

        } else if (getItemViewType(position) == 0 && viewTitle != null) {
            if (activeUser.getType().equals(CircleSmoothUserTopFragment.COSERTITLE)) {
                viewTitle.title.setText(mContext.getString(R.string.high_coser));
                if (map.get(CircleSmoothUserTopFragment.COSER) == 0) {
                    viewTitle.empty.setVisibility(View.VISIBLE);
                } else {
                    viewTitle.empty.setVisibility(View.GONE);
                }
            } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.DRAWERTITLE)) {
                viewTitle.title.setText(mContext.getString(R.string.high_drawer));
                if (map.get(CircleSmoothUserTopFragment.DRAWER) == 0) {
                    viewTitle.empty.setVisibility(View.VISIBLE);
                } else {
                    viewTitle.empty.setVisibility(View.GONE);
                }
            } else {
                viewTitle.title.setText(mContext.getString(R.string.high_writer));
                if (map.get(CircleSmoothUserTopFragment.WRITER) == 0) {
                    viewTitle.empty.setVisibility(View.VISIBLE);
                } else {
                    viewTitle.empty.setVisibility(View.GONE);
                }
            }
        } else {
            convertView.findViewById(R.id.find_more).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (activeUser.getType().contains(CircleSmoothUserTopFragment.COSER)) {
                        gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.COSER);
                    } else if (activeUser.getType().contains(CircleSmoothUserTopFragment.WRITER)) {
                        gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.WRITER);
                    } else {
                        gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.DRAWER);
                    }
                }
            });
        }

        return convertView;
    }

}
